/*
 * Created by JFormDesigner on Sun Dec 25 21:17:52 ICT 2011
 */

package View;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.MouseAdapter;

import javax.swing.*;
import javax.swing.table.TableModel;

import Entity.Department;

public class DepartmentManageView extends JPanel {

	private static final long serialVersionUID = 1L;
	public DepartmentManageView() {
		initComponents();
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		scrollPane1 = new JScrollPane();
		tblListDepartment = new JTable();
		panel1 = new JPanel();
		label1 = new JLabel();
		txtDepartmentName = new JTextField();
		label2 = new JLabel();
		txtDepartmentAllowance = new JTextField();
		panel2 = new JPanel();
		btAdd = new JButton();
		btWrite = new JButton();
		btEdit = new JButton();
		btDelete = new JButton();
		btExit = new JButton();

		//======== this ========
		setLayout(new BorderLayout());

		//======== scrollPane1 ========
		{
			scrollPane1.setViewportView(tblListDepartment);
		}
		add(scrollPane1, BorderLayout.CENTER);

		//======== panel1 ========
		{
			panel1.setLayout(new GridBagLayout());
			((GridBagLayout)panel1.getLayout()).columnWidths = new int[] {129, 258, 0};
			((GridBagLayout)panel1.getLayout()).rowHeights = new int[] {0, 0, 0, 0};
			((GridBagLayout)panel1.getLayout()).columnWeights = new double[] {0.0, 0.0, 1.0E-4};
			((GridBagLayout)panel1.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 1.0E-4};

			//---- label1 ----
			label1.setText("Department Name");
			panel1.add(label1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 5), 0, 0));
			panel1.add(txtDepartmentName, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 0), 0, 0));

			//---- label2 ----
			label2.setText("Department Allowance");
			panel1.add(label2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 5), 0, 0));
			panel1.add(txtDepartmentAllowance, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 0), 0, 0));
		}
		add(panel1, BorderLayout.NORTH);

		//======== panel2 ========
		{
			panel2.setLayout(new FlowLayout());

			//---- btAdd ----
			btAdd.setText("Add");
			panel2.add(btAdd);

			//---- btWrite ----
			btWrite.setText("Write");
			panel2.add(btWrite);

			//---- btEdit ----
			btEdit.setText("Edit");
			panel2.add(btEdit);

			//---- btDelete ----
			btDelete.setText("Delete");
			panel2.add(btDelete);

			//---- btExit ----
			btExit.setText("Exit");
			panel2.add(btExit);
		}
		add(panel2, BorderLayout.SOUTH);
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JScrollPane scrollPane1;
	private JTable tblListDepartment;
	private JPanel panel1;
	private JLabel label1;
	private JTextField txtDepartmentName;
	private JLabel label2;
	private JTextField txtDepartmentAllowance;
	private JPanel panel2;
	private JButton btAdd;
	private JButton btWrite;
	private JButton btEdit;
	private JButton btDelete;
	private JButton btExit;
	// JFormDesigner - End of variables declaration  //GEN-END:variables

	public void setModel(TableModel model) {
		this.tblListDepartment.setModel(model);
	}

	public void doAdd(ActionListener listener) {
		this.btAdd.addActionListener(listener);
	}

	public void doWrite(ActionListener listener) {
		this.btWrite.addActionListener(listener);
	}
	
	public void doEdit(ActionListener listener) {
		this.btEdit.addActionListener(listener);
	}

	public void doDelete(ActionListener listener) {
		this.btDelete.addActionListener(listener);
	}
	
	
	public int getSelectedItem() {
		return this.tblListDepartment.getSelectedRow();
	}

	public void setEnableControl(boolean enabled) {
		this.txtDepartmentAllowance.setEnabled(enabled);
		this.txtDepartmentName.setEnabled(enabled);	
	}

	public Department getData() {
		return new Department(
				this.txtDepartmentName.getText(),
				new Float(this.txtDepartmentAllowance.getText())
		);
	}

	public void display(Department department) {
		this.txtDepartmentName.setText(department.getName());
		this.txtDepartmentAllowance.setText(department.getAllowance());
	}

	public void doSelectTable(MouseAdapter mouseListener) {
		this.tblListDepartment.addMouseListener(mouseListener);
	}

	public void doSelectTable(KeyAdapter keyListener) {
		this.tblListDepartment.addKeyListener(keyListener);
	}
	
	public void doExit(ActionListener listener) {
		this.btExit.addActionListener(listener);
	}
}
